package com.sj.purchase.modules.purchase.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by icodebug on 17-5-22.
 */
@Entity
@JsonIgnoreProperties({"new"})
public class Purchase {

    @Id
    private PurchaseId id;

    private Integer finalPurchase=0;//最终采购 = (1+2) / 2

    private Integer finalPurchase1=0;//最终采购1 = 最近三天销售平均数×安全天数-实际库存-在途

    private Integer finalPurchase2=0;//最终采购2 = 昨天销量×安全天数-实际库存-在途

    private Double lastPrice=0d;//最后一次采购价

    private Double stockCost=0d;//库存成本价



    //@Formula("(select sum(sp.yesterday_sales) from purchase as p join purchase.stock_price as sp on p.sku_no=sp.purchase_sku_no where DATEDIFF(p.create_date,NOW())=-3 and p.sku_no= id.skuNo ")//统计最近三天这个编码的商品所有销量的平均数
    //@Formula("(select sum(sp.yesterday_sales) from purchase.stock_price sp where sp.purchase_create_date between date_sub(id.createDate, interval '2' day) and id.createDate AND sp.purchase_sku_no = id.skuNo )")//统计最近三天这个编码的商品所有销量的平均数
    //private Integer threeDaySales;//最近三天总销量

    @OneToMany(mappedBy="purchase",fetch= FetchType.EAGER,orphanRemoval = true,cascade = CascadeType.ALL)
    private Set<StockPrice> stockPrices = new HashSet<>();


    public PurchaseId getId() {
        return id;
    }

    public void setId(PurchaseId id) {
        this.id = id;
    }

    public Integer getFinalPurchase() {
        return finalPurchase;
    }

    public void setFinalPurchase(Integer finalPurchase) {
        this.finalPurchase = finalPurchase;
    }

    public Integer getFinalPurchase2() {
        return finalPurchase2;
    }

    public void setFinalPurchase2(Integer finalPurchase2) {
        this.finalPurchase2 = finalPurchase2;
    }

    public Double getLastPrice() {
        return lastPrice;
    }

    public void setLastPrice(Double lastPrice) {
        this.lastPrice = lastPrice;
    }

    public Double getStockCost() {
        return stockCost;
    }

    public void setStockCost(Double stockCost) {
        this.stockCost = stockCost;
    }

    public Set<StockPrice> getStockPrices() {
        return stockPrices;
    }

    public void setStockPrices(Set<StockPrice> stockPrices) {
        this.stockPrices = stockPrices;
    }

    public Integer getFinalPurchase1() {
        return finalPurchase1;
    }

    public void setFinalPurchase1(Integer finalPurchase1) {
        this.finalPurchase1 = finalPurchase1;
    }

    //获取最近三天销量平均数
    private Double getThreeDaySalesAVG(){
        Integer threeDaySales = stockPrices.stream().map((sp)->sp.getThreeDaySales()).reduce((sum,yds)->sum+yds.intValue()).get();
        //System.out.println(JSON.toJSONString(id)+"   三天总销量："+threeDaySales);
        return threeDaySales.doubleValue()/3;
    }


    /**
     * 最终采购1 = 最近三天销售平均数×安全天数-实际库存-在途
     */
    public void setFinalPurchase12(){
        //实际库存
        Integer nowStock = stockPrices.stream().map((sp)->sp.getNowStock()-sp.getNotDeploy()-sp.getNotPending()).reduce((sum,yds)->sum+yds.intValue()).get();
        //在途
        Integer passage =stockPrices.stream().map((sp)->sp.getPassage()).reduce((sum,yds)->sum+yds.intValue()).get();



        this.finalPurchase1 = securityDay(getThreeDaySalesAVG()) - nowStock -passage;

        //System.out.println(JSON.toJSONString(id)+"   实际库存："+nowStock+"    在途库存："+passage);

        //昨日总销量
        Integer yesterdaySales = stockPrices.stream().map((sp)->sp.getYesterdaySales()).reduce((sum,yds)->sum+yds.intValue()).get();
        this.finalPurchase2 = securityDay(yesterdaySales.doubleValue())- nowStock -passage;

        this.finalPurchase = (this.finalPurchase1+this.finalPurchase2)/2;
    }

    /**
     * 安全天数和销量平均数的积
     * @return
     */
    private Integer securityDay(Double salesAVG){
        if (salesAVG<5){
            return (int)Math.ceil(salesAVG*3);
        }else if (5 <= salesAVG && salesAVG < 30){
            return (int)Math.ceil(salesAVG*5);
        }else {
            return (int)Math.ceil(salesAVG*7);
        }
    }


}
